home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils text / ReadDocJ 1.1 / ReadDocJ.exe / Source / DatabaseHeader.java < prev    next >
Encoding:
Java Source  |  1999-05-27  |  10.4 KB  |  365 lines

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * <code>DatabaseHeader</code> class is used to read/write Palm database header records
  6.  * NOTE: Information about the various fields was taken from <a href="www.roadcoders.com/pdb.html">Road Coders</a>
  7.  * @author Jeffrey A. Krzysztow
  8.  * @author Pat Beirne
  9.  * @version 1.3
  10.  */
  11. public class DatabaseHeader {
  12.     /**
  13.      * database ID for TealDoc PilotDoc reader
  14.      * @since 1.0
  15.      */
  16.     public final static int TealDocID = 0x546c4463;    // 'TlDc'
  17.     /**
  18.      * database ID for generic PilotDoc reader
  19.      * @since 1.0
  20.      */
  21.     public final static int ReaderID = 0x52454164;    // 'REAd'
  22.  
  23.     /**
  24.      * indicates type of data stored in database
  25.      * @since 1.0
  26.      */
  27.     public final static int TEXt = 0x54455874;        // 'TEXt'
  28.  
  29.  
  30.     /**
  31.      * name of database
  32.      * @since 1.0
  33.      */
  34.     public String name = "";                    // 32
  35.  
  36.     /**
  37.      * attribute of database
  38.      *    0x0002 Read-Only
  39.      *    0x0004 Dirty AppInfoArea
  40.      *    0x0008 Backup this database (i.e. no conduit exists)
  41.      *    0x0010 (16 decimal) Okay to install newer over existing copy, if present on PalmPilot
  42.      *    0x0020 (32 decimal) Force the PalmPilot to reset after this database is installed
  43.      *    0x0040 (64 decimal) Don't allow copy of file to be beamed to other Pilot.
  44.      * @since 1.0
  45.      */
  46.     public short attribute = 0;                    // 2 Word
  47.  
  48.     /**
  49.      * version of database
  50.      * defined by application
  51.      * @since 1.0
  52.      */
  53.     public short version = 0;                    // 2 Word
  54.  
  55.     /**
  56.      * creation date of database
  57.      * Expressed as the number of seconds since January 1, 1904.
  58.      * <b>The database will not install if this value is zero.</b>
  59.      * @since 1.0
  60.      */
  61.     public int creationDate = 0;                // 4 DWord
  62.  
  63.     /**
  64.      * last modification date of database
  65.      * Expressed as the number of seconds since January 1, 1904.
  66.      * <b>The database will not install if this value is zero.</b>
  67.      * @since 1.0
  68.      */
  69.     public int modificationDate = 0;            // 4 DWord
  70.  
  71.     /**
  72.      * last date database was HotSynced
  73.      * Expressed as the number of seconds since January 1, 1904.
  74.      * The database will install if this value is zero.
  75.      * @since 1.0
  76.      */
  77.     public int lastBackupDate = 0;                // 4 DWord
  78.  
  79.     /**
  80.      * modification number
  81.      * Set to zero
  82.      * @since 1.0
  83.      */
  84.     public int modificationNumber = 0;            // 4 DWord
  85.  
  86.     /**
  87.      * appInfoID
  88.      * The byte number in the PDB file (counting from zero) at
  89.      * which the AppInfoArea is located. This must be the first
  90.      * entry in the Data portion of the PDB file. If this
  91.      * database does not have an AppInfoArea, set this value to
  92.      * zero.
  93.      * @since 1.0
  94.      */
  95.     public int appInfoID = 0;                    // 4 DWord
  96.  
  97.     /**
  98.      * sortInfoID
  99.      * The byte number in the PDB file (counting from zero) at
  100.      * which the SortInfoArea is located. This must be placed
  101.      * immediately after the AppInfoArea, if one exists, within
  102.      * the Data portion of the PDB file. If this database does
  103.      * not have a SortInfoArea, set this value to zero. Do not
  104.      * use this.
  105.      * @since 1.0
  106.      */
  107.     public int sortInfoID = 0;                    // 4 DWord
  108.  
  109.     /**
  110.      * ID of application that can update database
  111.      * @since 1.0
  112.      */
  113.     public int typeID = 0;                        // 4 DWord
  114.  
  115.     /**
  116.      * ID of application
  117.      * @since 1.0
  118.      */
  119.     public int creatorID = 0;                    // 4 DWord
  120.  
  121.     /**
  122.      * uniqueIDSeed
  123.      * This is used to generate the Unique ID number of
  124.      * subsequent records. This should be set to zero.
  125.      * @since 1.0
  126.      */
  127.     public int uniqueIDSeed = 0;                // 4 DWord
  128.  
  129.     /**
  130.      * nextRecordListID
  131.      * Set this to zero. The element is used only in the
  132.      * in-memory representation of a PDB file, but exists
  133.      * in the external version for consistency.
  134.      * @since 1.0
  135.      */
  136.     public int nextRecordListID = 0;            // 4 DWord
  137.  
  138.     /**
  139.      * number of records in database
  140.      * @since 1.0
  141.      */
  142.     public short numRecords = 0;                // 2 Word
  143.                                                 // 78 bytes for Database Header
  144.  
  145.     /**
  146.      * Default constructor
  147.      * @since 1.1
  148.      */
  149.     DatabaseHeader() {
  150.         // find the current time, and convert into number of seconds since Jan 1, 1904
  151.         Calendar cl = Calendar.getInstance();
  152.         Date now = new Date();
  153.         creationDate = (int)((now.getTime() + cl.get(Calendar.ZONE_OFFSET) + cl.get(Calendar.DST_OFFSET))
  154.             / 1000 + SecondsSince1904);
  155.         modificationDate = creationDate;
  156.     }
  157.  
  158.     /**
  159.      * The size of the DatabaseHeader in bytes
  160.      * @returns the number of bytes in the DatabaseHeader
  161.      * @since 1.0
  162.      */
  163.     public static int getSize() {
  164.         return(78);
  165.     }
  166.  
  167.     /**
  168.      * Reads the DatabaseHeader from the DataInput
  169.      * @param the DataInput to read from
  170.      * @since 1.0
  171.      */
  172.     public void read(DataInput di) throws IOException {
  173.         byte[] b = new byte[32];
  174.         di.readFully(b);
  175.         name = new String(b);
  176.         int i = name.indexOf('\0');
  177.         if(i >= 0) {
  178.             name = name.substring(0,i);
  179.         }
  180.         attribute = di.readShort();
  181.         version = di.readShort();
  182.         creationDate = di.readInt();
  183.         modificationDate = di.readInt();
  184.         lastBackupDate = di.readInt();
  185.         modificationNumber = di.readInt();
  186.         appInfoID = di.readInt();
  187.         sortInfoID = di.readInt();
  188.         typeID = di.readInt();
  189.         creatorID = di.readInt();
  190.         uniqueIDSeed = di.readInt();
  191.         nextRecordListID = di.readInt();
  192.         numRecords = di.readShort();
  193.     }
  194.  
  195.     /**
  196.      * Writes the DatabaseHeader to the DataInput
  197.      * @param the DataInput to write to
  198.      * @since 1.0
  199.      */
  200.     public void write(DataOutput out) throws IOException {
  201.         byte[] b = new byte[32];
  202.         byte[] bName = name.getBytes();
  203.         for(int x=0; x < b.length; x++) {
  204.             if(x < bName.length) {
  205.                 b[x] = bName[x];
  206.             }
  207.             else {
  208.                 b[x] = 0;
  209.             }
  210.         }
  211.         out.write(b);
  212.         out.writeShort(attribute);
  213.         out.writeShort(version);
  214.         out.writeInt(creationDate);
  215.         out.writeInt(modificationDate);
  216.         out.writeInt(lastBackupDate);
  217.         out.writeInt(modificationNumber);
  218.         out.writeInt(appInfoID);
  219.         out.writeInt(sortInfoID);
  220.         out.writeInt(typeID);
  221.         out.writeInt(creatorID);
  222.         out.writeInt(uniqueIDSeed);
  223.         out.writeInt(nextRecordListID);
  224.         out.writeShort(numRecords);
  225.     }
  226.  
  227.     /**
  228.      * override Object.toString()
  229.      * @since 1.0
  230.      */
  231.     public String toString()  {
  232.         char[] creatorIDa = new char[4];
  233.         creatorIDa[0] = (char)((creatorID >> 24) & 0xff);
  234.         creatorIDa[1] = (char)((creatorID >> 16) & 0xff);
  235.         creatorIDa[2] = (char)((creatorID >> 8) & 0xff);
  236.         creatorIDa[3] = (char)(creatorID & 0xff);
  237.         char[] typeIDa = new char[4];
  238.         typeIDa[0] = (char)((typeID >> 24) & 0xff);
  239.         typeIDa[1] = (char)((typeID >> 16) & 0xff);
  240.         typeIDa[2] = (char)((typeID >> 8) & 0xff);
  241.         typeIDa[3] = (char)(typeID & 0xff);
  242.         return ">> DatabaseHeader <<"
  243.             + "\nname = `" + name + "`"
  244.             + "\nattribute = " + attribute
  245.             + "\nversion = " + version
  246.             + "\ncreationDate = " + creationDate
  247.             + "\nmodificationDate = " + modificationDate
  248.             + "\nlastBackupDate = " + lastBackupDate
  249.             + "\nmodificationNumber = " + modificationNumber
  250.             + "\nappInfoID = " + appInfoID
  251.             + "\nsortInfoID = " + sortInfoID
  252.             + "\ntypeID = " + typeID + " 0x" + Integer.toHexString(typeID) + " `" + new String(typeIDa) + "`"
  253.             + "\ncreatorID = " + creatorID + " 0x" + Integer.toHexString(creatorID) + " `" + new String(creatorIDa) + "`"
  254.             + "\nuniqueIDSeed = " + uniqueIDSeed + "\nnextRecordListID = " + nextRecordListID
  255.             + "\nnumRecords = " + numRecords;
  256.     }
  257.  
  258.     /**
  259.      * Sets the modification date member to now
  260.      * @since 1.2
  261.      */
  262.     public void setModificationDate() {
  263.         // find the current time, and convert into number of seconds since Jan 1, 1904
  264.         Calendar cl = Calendar.getInstance();
  265.         Date now = new Date();
  266.         modificationDate = (int)((now.getTime() + cl.get(Calendar.ZONE_OFFSET) + cl.get(Calendar.DST_OFFSET))
  267.             / 1000 + SecondsSince1904);
  268.     }
  269.  
  270.     /**
  271.      * returns the modification date as a Date
  272.      * @returns Date indicating modification date
  273.      * @since 1.2
  274.      */
  275.     public Date getModificationDate() {
  276.         Calendar cl = Calendar.getInstance();
  277.         long temp = new Integer(modificationDate).longValue();
  278.         if(temp < 0) {
  279.             temp += ((long) 1) << 32;
  280.         }
  281.         return new Date((temp - SecondsSince1904) * 1000 - cl.get(Calendar.ZONE_OFFSET) - cl.get(Calendar.DST_OFFSET));
  282.     }
  283.  
  284.     /**
  285.      * returns the creation date as a Date
  286.      * @returns Date indicating creation date
  287.      * @since 1.2
  288.      */
  289.     public Date getCreationDate() {
  290.         Calendar cl = Calendar.getInstance();
  291.         long temp = new Integer(creationDate).longValue();
  292.         if(temp < 0) {
  293.             temp += ((long) 1) << 32;
  294.         }
  295.         return new Date((temp - SecondsSince1904) * 1000 - cl.get(Calendar.ZONE_OFFSET) - cl.get(Calendar.DST_OFFSET));
  296.     }
  297.  
  298.     /**
  299.      * Seconds since 1904
  300.      * @see SecondsSince1904
  301.      * @since 1.2
  302.      */
  303.     private final static long SecondsSince1904 = 2082844800;
  304.  
  305.     /**
  306.      * converts the String representation of typeID to an int representation
  307.      * @param typeID String representation of typeID
  308.      * @return int representation of typeID
  309.      * @since 1.3
  310.      */
  311.     public static int convertStringToTypeID(String typeID) {
  312.         byte typeIDa[] = typeID.getBytes();
  313.         return ((0xff & typeIDa[0]) << 24)
  314.             + ((0xff & typeIDa[1]) << 16)
  315.             + ((0xff & typeIDa[2]) << 8)
  316.             + (0xff & typeIDa[3]);
  317.     }
  318.  
  319.     /**
  320.      * converts the int representation of typeID to a String representation
  321.      * @param typeID int representation of typeID
  322.      * @return String representation of typeID
  323.      * @since 1.3
  324.      */
  325.     public static String convertIntToStringTypeID(int typeID)     {
  326.         char[] typeIDa = new char[4];
  327.         typeIDa[0] = (char)((typeID >> 24) & 0xff);
  328.         typeIDa[1] = (char)((typeID >> 16) & 0xff);
  329.         typeIDa[2] = (char)((typeID >> 8) & 0xff);
  330.         typeIDa[3] = (char)(typeID & 0xff);
  331.         return new String(typeIDa);
  332.     }
  333.  
  334.     /**
  335.      * converts the String representation of creatorID to an int representation
  336.      * @param creatorID String representation of creatorID
  337.      * @return int representation of creatorID
  338.      * @since 1.3
  339.      */
  340.     public static int convertStringToCreatorID(String creatorID) {
  341.         byte creatorIDa[] = creatorID.getBytes();
  342.         return ((0xff & creatorIDa[0]) << 24)
  343.             + ((0xff & creatorIDa[1]) << 16)
  344.             + ((0xff & creatorIDa[2]) << 8)
  345.             + (0xff & creatorIDa[3]);
  346.     }
  347.  
  348.     /**
  349.      * converts the int representation of creatorID to a String representation
  350.      * @param creatorID int representation of creatorID
  351.      * @return String representation of creatorID
  352.      * @since 1.3
  353.      */
  354.     public static String convertIntToStringCreatorID(int creatorID)     {
  355.         char[] creatorIDa = new char[4];
  356.         creatorIDa[0] = (char)((creatorID >> 24) & 0xff);
  357.         creatorIDa[1] = (char)((creatorID >> 16) & 0xff);
  358.         creatorIDa[2] = (char)((creatorID >> 8) & 0xff);
  359.         creatorIDa[3] = (char)(creatorID & 0xff);
  360.         return new String(creatorIDa);
  361.     }
  362.  
  363. }
  364.  
  365.